<?php

namespace App\Http\Controllers\Admin;

use App\Models\Config;
use App\Models\Hospital;
use App\Models\Store;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class StoreController extends BaseController
{
    /**
     * 首页数据
     * @param Request $request
     * @return array
     */
    public function index(Request $request)
    {
        $params = $request->all();
        if (isset($params['location'])){
            $location = explode(',', $params['location']);
            $lng = $location[0];
            $lat = $location[1];
        }

        $data = Store::query()->get()->toArray();
        foreach ($data as $key => $value){
            if(isset($params['location'])){
                $data[$key]['distance'] = $this->getDistance($value['location_lng'], $value['location_lat'], $lng, $lat);
            }
        }
        return $this->yueSuccess($data);
    }

    public function getStoreData($id, Request $request)
    {
        $data = Store::query()->where('store_id', $id)->first();
        return $this->success($data);
    }

    public function configIndex()
    {
        $data = Config::query()->where('id', 1)->first();
        return $this->success($data);
    }

    public function addStore(Request $request)
    {
        $params = $request->all();

        if (!isset($params['opening_time']) || $params['opening_time'] == ''){
            $params['opening_time'] = "10:00";
        }
        if (!isset($params['closing_time']) || $params['closing_time'] == ''){
            $params['closing_time'] = "21:00";
        }
        $params['store_server_labels'] = '[{"summary": "免费试吸", "icon_url": "https://oss-static.relxtech.com/shopkeeper/file/2020/07/20/4a828469-d1b7-4b04-87b9-2584a9193bf8%E4%BD%93%E9%AA%8C%E6%9C%8D%E5%8A%A1%403x.png"}, {"summary": "周边在售", "icon_url": "https://oss-static.relxtech.com/shopkeeper/file/2020/07/20/0bbb318b-b60a-492f-96eb-d18dddb46070DIY%403x.png"}, {"summary": "满198元包邮", "icon_url": "https://oss-static.relxtech.com/shopkeeper/file/2020/07/20/6eee9c40-a1ca-4f97-9edd-520379de9956%E5%90%8C%E5%9F%8E%E9%80%81%E8%BE%BE%403x.png"}, {"summary": "满495元包闪送", "icon_url": "https://oss-static.relxtech.com/shopkeeper/file/2020/07/20/6eee9c40-a1ca-4f97-9edd-520379de9956%E5%90%8C%E5%9F%8E%E9%80%81%E8%BE%BE%403x.png"}]';
        $params['store_server_labels2'] = '[{"summary": "免费试吸", "icon_url": "https://oss-static.relxtech.com/shopkeeper/file/2020/07/20/4a828469-d1b7-4b04-87b9-2584a9193bf8%E4%BD%93%E9%AA%8C%E6%9C%8D%E5%8A%A1%403x.png"}, {"summary": "周边在售", "icon_url": "https://oss-static.relxtech.com/shopkeeper/file/2020/07/20/0bbb318b-b60a-492f-96eb-d18dddb46070DIY%403x.png"}, {"summary": "满198元包邮", "icon_url": "https://oss-static.relxtech.com/shopkeeper/file/2020/07/20/6eee9c40-a1ca-4f97-9edd-520379de9956%E5%90%8C%E5%9F%8E%E9%80%81%E8%BE%BE%403x.png"}, {"summary": "满495元包闪送", "icon_url": "https://oss-static.relxtech.com/shopkeeper/file/2020/07/20/6eee9c40-a1ca-4f97-9edd-520379de9956%E5%90%8C%E5%9F%8E%E9%80%81%E8%BE%BE%403x.png"}]';
        $location = $this->getAddressLng($params['store_address']);
        $params['view_phone'] = 0;
        $params['location_lng'] = $location['lng'];
        $params['location_lat'] = $location['lat'];
        $params['store_lng_lat'] =  $location['lng'] . ',' . $location['lat'];
        $params['real_lng_lat'] = $location['lng'] . ',' . $location['lat'];


        $result = Store::query()->create($params);


        return $this->success($result);
    }

    public function getAddressLng($address)
    {
        $url = "http://api.map.baidu.com/geocoding/v3/?address=$address&output=json&ak=wmIVIGn3wxBuUIaUVl2dAjjwVf4tjEWR";
        $result = file_get_contents($url);
        $result = json_decode($result, true);
        if ($result['status'] == 0){
            $location = $result['result']['location'];
        }else{
            $location['lng'] = 0;
            $location['lat'] = 0;

        }

        return $location;
    }

    public function delete($id, Request $request)
    {
        $result = Store::query()->where('store_id', $id)->delete();
        return $this->success($result);

    }

    public function getBanner()
    {
        $result = [
            'https://kong.api.effort.fun/uploads/title/2106222157101624370230.jpg'
        ];
        return $this->success($result);
    }


    public function getProductInfo()
    {
        $result[0]['title'] = 'FEOK01';
        $result[0]['url'] = 'https://kong.api.effort.fun/uploads/title/2106222127221624368442.png';
        $result[1]['title'] = 'FEOK02';
        $result[1]['url'] = 'https://kong.api.effort.fun/uploads/title/2106222128171624368497.png';
        $result[2]['title'] = 'FEOK03';
        $result[2]['url'] = 'https://kong.api.effort.fun/uploads/title/2106222129031624368543.png';
        $result[3]['title'] = 'FEOK04';
        $result[3]['url'] = 'https://kong.api.effort.fun/uploads/title/2106222129221624368562.png';
        $result[4]['title'] = 'FEOK05';
        $result[4]['url'] = 'https://kong.api.effort.fun/uploads/title/2106222129361624368576.png';
        $result[5]['title'] = 'FEOK06';
        $result[5]['url'] = 'https://kong.api.effort.fun/uploads/title/2106222129471624368587.png';
        return $this->success($result);
    }

    public function getDistance($lng1, $lat1, $lng2, $lat2)
    {
        $EARTH_RADIUS = 6378137;   //地球半径
        $RAD = pi() / 180.0;
        $radLat1 = $lat1 * $RAD;
        $radLat2 = $lat2 * $RAD;
        $a = $radLat1 - $radLat2;    // 两点纬度差
        $b = ($lng1 - $lng2) * $RAD;  // 两点经度差
        $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));
        $s = $s * $EARTH_RADIUS;
        $s = round($s * 10000) / 10000;
        return $s;
    }
}